package com.lanou3g.dllo.baidumusic.musiclibrary.playlist.playlistdetail;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.OnRemoveListener;
import com.lanou3g.dllo.baidumusic.tools.SingleVolley;

import java.util.List;

/**
 * Created by dllo on 16/6/23.
 */
public class PlaylistDetailAdapter extends RecyclerView.Adapter<PlaylistDetailAdapter.MyViewHolder>{

    private PlaylistDetailBean playlistDetailBean;
    private List<PlaylistDetailBean.ContentBean> list;
    private MyRvOnClickListener myRvOnClickListener;
    private OnRemoveListener onRemoveListener;

    public void setOnRemoveListener(OnRemoveListener onRemoveListener) {
        this.onRemoveListener = onRemoveListener;
    }

    public void setMyRvOnClickListener(MyRvOnClickListener myRvOnClickListener) {
        this.myRvOnClickListener = myRvOnClickListener;
    }

    public void setPlaylistDetailBean(PlaylistDetailBean playlistDetailBean) {
        this.playlistDetailBean = playlistDetailBean;
        list = playlistDetailBean.getContent();
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.context).inflate(R.layout.item_playlistdetail,parent,false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.playlistdetailSongsnameTv.setText(list.get(position).getTitle());
        holder.playlistdetailSingerTv.setText(list.get(position).getAuthor());
        holder.playlistDetailLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当itemView被点击的时候
                // getLayoutPosition是用来获得当前点击的position
                int position = holder.getLayoutPosition();
                // 调用接口对象的onClick方法
                myRvOnClickListener.onClick(position);
            }
        });

        holder.playlistDetailMoreBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当itemView被点击的时候
                // getLayoutPosition是用来获得当前点击的position
                int position = holder.getLayoutPosition();
                // 调用接口对象的onClick方法
                onRemoveListener.onRemoveClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0: list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout playlistDetailLayout;
        TextView playlistdetailSongsnameTv,playlistdetailSingerTv;
        ImageView playlistDetailMoreBtn;

        public MyViewHolder(View itemView) {
            super(itemView);
            playlistdetailSongsnameTv = (TextView) itemView.findViewById(R.id.playlistdetail_songsname_tv);
            playlistdetailSingerTv = (TextView) itemView.findViewById(R.id.playlistdetail_singer_tv);
            playlistDetailLayout = (LinearLayout) itemView.findViewById(R.id.playlistdetail_layout);
            playlistDetailMoreBtn = (ImageView) itemView.findViewById(R.id.playlistdetail_more_btn);
        }
    }
}
